package com.xls.resource.dialog.utils.download;

import com.lzz.bottomencapsulation.entity.VersionEntity;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PackageUtil {
    private static String[] getArray(String packageString){
        return packageString.split(".");
    }

    /**
     * 获取APP开发者编号
     * @param packageString
     * @return
     */
    public static String getAppOwnerCode(String packageString){
        String[] array = getArray(packageString);
        if(array.length>=5){
            return array[4];
        }
        return "";
    }

    /**
     * 获取APP名(应用/游戏)
     * @param packageString
     * @return
     */
    public static String getAppCode(String packageString){
        String[] array = getArray(packageString);
        if(array.length>=6){
            return array[5];
        }
        return "";
    }

    /**
     * 获取分发渠道名(盒子名)
     * @param packageString
     * @return
     */
    public static String getDistributionCode(String packageString){
        String[] array = getArray(packageString);
        if(array.length>=7){
            return array[6];
        }
        return "";
    }

    public static VersionEntity getPackageInfo(String apkFilePath){
        File file = new File(apkFilePath);
        VersionEntity appVersion = new VersionEntity();
        appVersion.setFileSize(file.length());
        appVersion.setMd5(computeMD5(file));
        return appVersion;
    }

    public static String computeMD5(File file) {
        DigestInputStream din = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            //第一个参数是一个输入流
            din = new DigestInputStream(new BufferedInputStream(new FileInputStream(file)), md5);

            byte[] b = new byte[1024];
            while (din.read(b) != -1);

            byte[] digest = md5.digest();
            StringBuilder result = new StringBuilder(byte2Hex(digest));

            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (din != null) {
                    din.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static String byte2Hex(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        // 遍历byte[]数组，将每个byte数字转换成16进制字符，再拼接起来成字符串
        for (int i = 0; i < bytes.length; i++) {
            // 每个byte转换成16进制字符时，bytes[i] & 0xff如果高位是0，输出将会去掉，所以+0x100(在更高位加1)，再截取后两位字符
            builder.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        return builder.toString();
    }
}
